//
//  BuyerDetailViewController.m
//  UGoods
//
//  Created by dllo on 16/1/21.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "BuyerDetailViewController.h"
#import "FindModel.h"
#import "BaseView.h"
#import "BaseLabel.h"
#import "LHAFNetworkTool.h"
#import "BuyerDetailCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "BuyerDetailFlowLayout.h"
#import "BuyerDetailHeaderReusableView.h"
#import "BuyDetailViewController.h"
#import "ConstantsDefine.h"
@interface BuyerDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArray; /**< 数据数组 */
@property (nonatomic, retain) UIImageView *backImageView;
@property (nonatomic, retain) UIView *backView;
@property (nonatomic, retain) NSDictionary *dic;

@end

@implementation BuyerDetailViewController

- (void)dealloc
{
    [_collectionView release];
    [_dataArray release];
    [_backImageView release];
    [_backView release];
    [_dic release];
    [super dealloc];
}

- (void)handleData {
    NSString *str = [NSString stringWithFormat:@"http://api.ujipin.com/v4/feature/%@?format=json&channel=appstore&idfa=1EF25F55-6367-4567-B0D5-82DF4F1557CD&user_id=470825&plantform=iphone&version=314", _model.feature_id];
    self.dic = [NSDictionary dictionary];
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        self.dic = resposeObject[@"data"];
        NSArray *array = resposeObject[@"data"][@"recommend"];
        self.dataArray = [FindModel modelHandleWithArray:array];
        
        [self createCollectionView];
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"解析失败：%@", error);
    }];
}

//  创建一个横向滑动的CollectionView
- (void)createCollectionView {
    
    // 创建ImageView毛玻璃效果
    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-self.view.frame.size.width / 3, -self.view.frame.size.height / 3, self.view.frame.size.width * 1.80, self.view.frame.size.height * 1.80)];
    self.backImageView.userInteractionEnabled = YES;
    
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:_dic[@"background"]] placeholderImage:[UIImage imageNamed:@"place3"]];
    [self.view addSubview:self.backImageView];
    [self.backImageView release];

    self.backImageView.contentMode = UIViewContentModeScaleAspectFit;
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:effectView];
    [effectView release];
    
//    self.backView = [[UIView alloc] initWithFrame:self.view.bounds];
//    _backView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.620];
//    [self.view addSubview:_backView];
    
    // 自定义的FlowLayout
    BuyerDetailFlowLayout *layout = [[BuyerDetailFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width - 60 * WIDTH, self.view.frame.size.height);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [effectView addSubview:_collectionView];
    [_collectionView release];
    [_collectionView registerClass:[BuyerDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[BuyerDetailHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

// 创建CollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BuyerDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    FindModel *model = _dataArray[indexPath.item];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = model;
    return cell;
}

// 设置头部区域
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    BuyerDetailHeaderReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    header.backgroundColor = [UIColor clearColor];
    [header.buyerImageView sd_setImageWithURL:[NSURL URLWithString:_dic[@"buyer"][@"avatar"]] placeholderImage:[UIImage imageNamed:@"iconfont-guanzhuliubianxing-5"]];
    header.buyerNameLabel.text = _dic[@"buyer"][@"name"];
    header.introLabel.text = [NSString stringWithFormat:@"“%@”", _dic[@"intro"]];
    header.titleLabel.text = _dic[@"title"];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5 * HEIGHT;// 字体的行间距
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15 * WIDTH],                                 NSParagraphStyleAttributeName:paragraphStyle,                                 NSForegroundColorAttributeName:[UIColor whiteColor]};
    header.guideTextView.attributedText = [[NSAttributedString alloc] initWithString:_dic[@"guide"] attributes:attributes];
    return header;
}


#pragma mark cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.frame.size.width - 60 * WIDTH, self.view.frame.size.height - 64 * HEIGHT - 60 * HEIGHT - 60 * HEIGHT);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FindModel *model = _dataArray[indexPath.item];
    BuyDetailViewController *buy = [[BuyDetailViewController alloc] init];
    buy.model = model;
    [self.navigationController pushViewController:buy animated:YES];
}

- (void)setModel:(FindModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [self handleData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"买手作业";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
